<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="所属楼栋id" prop="buildingId">
      <el-input v-model="dataForm.buildingId" placeholder="所属楼栋id"></el-input>
    </el-form-item>
    <el-form-item label="安全检查员id" prop="safetyInspectorId">
      <el-input v-model="dataForm.safetyInspectorId" placeholder="安全检查员id"></el-input>
    </el-form-item>
    <el-form-item label="灭火器状态" prop="fireExtinguisherStatus">
      <el-input v-model="dataForm.fireExtinguisherStatus" placeholder="灭火器状态"></el-input>
    </el-form-item>
    <el-form-item label="消防栓状态" prop="fireHydrantStatus">
      <el-input v-model="dataForm.fireHydrantStatus" placeholder="消防栓状态"></el-input>
    </el-form-item>
    <el-form-item label="火灾报警器状态" prop="fireAlarmStatus">
      <el-input v-model="dataForm.fireAlarmStatus" placeholder="火灾报警器状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          buildingId: '',
          safetyInspectorId: '',
          fireExtinguisherStatus: '',
          fireHydrantStatus: '',
          fireAlarmStatus: ''
        },
        dataRule: {
          buildingId: [
            { required: true, message: '所属楼栋id不能为空', trigger: 'blur' }
          ],
          safetyInspectorId: [
            { required: true, message: '安全检查员id不能为空', trigger: 'blur' }
          ],
          fireExtinguisherStatus: [
            { required: true, message: '灭火器状态不能为空', trigger: 'blur' }
          ],
          fireHydrantStatus: [
            { required: true, message: '消防栓状态不能为空', trigger: 'blur' }
          ],
          fireAlarmStatus: [
            { required: true, message: '火灾报警器状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$request({
              url: `/house/floor/info/${this.dataForm.id}`,
              method: 'get',
              params: {}
            }).then((res) => {
              if (res && res.code === 200) {
                this.dataForm.buildingId = res.floor.buildingId
                this.dataForm.safetyInspectorId = res.floor.safetyInspectorId
                this.dataForm.fireExtinguisherStatus = res.floor.fireExtinguisherStatus
                this.dataForm.fireHydrantStatus = res.floor.fireHydrantStatus
                this.dataForm.fireAlarmStatus = res.floor.fireAlarmStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$request({
              url: `/house/floor/${!this.dataForm.id ? 'save' : 'update'}`,
              method: 'post',
              data: {
                'id': this.dataForm.id || undefined,
                'buildingId': this.dataForm.buildingId,
                'safetyInspectorId': this.dataForm.safetyInspectorId,
                'fireExtinguisherStatus': this.dataForm.fireExtinguisherStatus,
                'fireHydrantStatus': this.dataForm.fireHydrantStatus,
                'fireAlarmStatus': this.dataForm.fireAlarmStatus
              }
            }).then((res) => {
              if (res && res.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
